package app.modules.common.exception;

import app.modules.common.constant.CommonErrorMsgConstants;

import javax.annotation.Nullable;

/**
 * <p>
 * 未授权的数据创建。
 * </p>
 *
 * @author mh.z
 */
public class UnauthorizedDataCreateException extends UnauthorizedDataOperateException {
    /** 数据类型 */
    protected Class<?> dataType;

    /** 默认错误代码 */
    public static final String DEFAULT_ERROR_CODE = "UNAUTHORIZED_DATA_CREATE_DENIED";
    /** 默认错误信息/错误信息key */
    public static final String DEFAULT_MESSAGE_OR_KEY = CommonErrorMsgConstants.UNAUTHORIZED_DATA_CREATE_DENIED;

    public UnauthorizedDataCreateException() {
        super(DEFAULT_ERROR_CODE, DEFAULT_MESSAGE_OR_KEY, null);
        this.dataType = null;
    }

    public Class<?> getDataType() {
        return dataType;
    }

    /**
     * 设置错误详情
     *
     * @param dataType
     * @param detail
     * @return
     */
    public UnauthorizedDataCreateException withErrorDetail(Class<?> dataType, @Nullable String detail) {
        this.dataType = dataType;
        this.errorDetail = buildErrorDetail(dataType, detail);

        return this;
    }

    /**
     * 生成错误详情
     *
     * @param dataType
     * @param detail
     * @return
     */
    public static String buildErrorDetail(Class<?> dataType, @Nullable String detail) {
        StringBuilder builder = new StringBuilder("create data '");
        builder.append(dataType.getSimpleName());
        builder.append("' denied");

        if (detail != null) {
            builder.append(", ");
            builder.append(detail);
        }

        return builder.toString();
    }

}
